﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LevelEditor.model.core;

namespace LevelEditor.controller
{
    public class EditorController
    {
        #region Variables
        private EditorView m_editorView;
        private EditorModel m_editorModel;

        #endregion

        #region Constructeurs
        public EditorController()
        {
        }

        #endregion

        #region Methodes appelees par le main
        public void init()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            m_editorModel = new EditorModel();
            m_editorView = new EditorView(this);

            m_editorModel.onStatutChanged += new EditorModel.StatutEventHandler(m_editorView.onStatutChanged);
            m_editorModel.onLibraryChanged += new EditorModel.LibraryEventHandler(m_editorView.onLibraryChanged);
            m_editorModel.onPGameObjectChanged += new EditorModel.PGameObjectEventHandler(m_editorView.onPGameObjectChanged);
            m_editorModel.onPGameObjectSelected += new EditorModel.PGameObjectSelectedEventHandler(m_editorView.onPGameObjectSelected);
            m_editorModel.onDoublePageChanged += new EditorModel.DoublePageEventHandler(m_editorView.onDoublePageChanged);
            m_editorModel.onLevelChanged += new EditorModel.LevelEventHandler(m_editorView.onLevelChanged);

            m_editorModel.loadLibrary();
            m_editorModel.addNewDoublePage();
        }

        public void displayViews()
        {
            Application.Run(m_editorView);
        }

        #endregion

        #region Notifications pour des changements sur les pages
        public void notifyNewDoublePage()
        {
            m_editorModel.addNewDoublePage();
        }

        public void notifySwitchToNextDoublePage()
        {
            if (m_editorModel.doublePageNb <= 1)
            {
                Console.WriteLine("Impossible de changer de page");
                return;
            }

            m_editorModel.switchToNextDoublePage();
        }

        public void notifySwitchToPreviousDoublePage()
        {
            if (m_editorModel.doublePageNb <= 1)
            {
                Console.WriteLine("Impossible de changer de page");
                return;
            }

            m_editorModel.switchToPreviousDoublePage();
        }

        public void notifyRemoveCurrentDoublePage()
        {
            if (m_editorModel.doublePageNb <= 1)
            {
                Console.WriteLine("Plus de pages a remove!");
                return;
            }

            m_editorModel.removeCurrentDoublePage();
        }

        public void notifyNewLevel()
        {
            m_editorModel.resetLevel();
        }

        #endregion

        #region Notifications pour les IO
        public void notifySaveLevel(String url)
        {
            if (m_editorModel.doublePageNb < 1)
            {
                Console.WriteLine("Rien a sauvegarder!");
                return;
            }

            m_editorModel.save(url);
        }

        public void notifyLoadLevel(string url)
        {
            m_editorModel.loadLevel(url);
        }

        #endregion

        #region Notifications pour des changements sur les pGameObjects
        public void notifyNewPGameObject( int key, int index )
        {
            if(m_editorModel.doublePageNb < 1)
            {
                Console.WriteLine("Pas de page disponible pour ajouter un pGameObject");
                return;
            }

            m_editorModel.addNewPGameObject(key, index);
        }

        public void notifyPGameObjectSelection(int index)
        {
            m_editorModel.selectPGameObject(index);
        }

        public void notifyPGameObjectParamChanged(int index, string boxName, float value)
        {
            m_editorModel.changePGameObjectParams(index, boxName, value);
        }

        public void notifyPGameObjectParamChanged(int index, string boxName, string value)
        {
            m_editorModel.changePGameObjectParams(index, boxName, value);
        }

        // Previent le controleur que l'utilisateur souhaite ajouter un PGO a la liste des PGO de l'objet index en parametre
        public void notifyNewLinkedPGO(int focusPGOIndex, int focusPGOPageIndex, int linkedPGOIndex, int linkedPGOPageIndex)
        {
            m_editorModel.addLinkedPGO(focusPGOIndex, focusPGOPageIndex, linkedPGOIndex, linkedPGOPageIndex);
        }

        // Previent le controleur que l'utilisateur souhaite supprimer un PGO a la liste des PGO de l'objet index en parametre
        public void notifyRemoveLinkedPGO(int focusPGOIndex, int focusPGOPageIndex, int linkedPGOToRemoveIndex)
        {
            Console.WriteLine("focusPGOIndex = " + focusPGOIndex);
            Console.WriteLine("focusPGOPageIndex = " + focusPGOPageIndex);
            Console.WriteLine("linkedPGOToRemoveIndex = " + linkedPGOToRemoveIndex);
        }

        public void notifyRemoveSelectedPGameObject(int index)
        {
            m_editorModel.removePGameObject(index);
        }

        #endregion

        #region Notifications pour des changements sur la lib
        public void notifyLibraryObjectSelection(int index)
        {
            m_editorModel.selectLibraryObject(index);
        }

        #endregion

        #region Autres methodes
        public void notifiyExit()
        {
            Application.Exit();
        }

        #endregion
    }
}
